using RaceHFTools.BluetoothLE.Bean;
using RaceHFTools.BluetoothLE.Bean.Module;
using System;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace RaceHFTools.Pages.Tweak
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TweakProPage : ContentPage
    {
        private readonly Bean bean;

        public TweakProPage(Bean bean)
        {
            InitializeComponent();

            this.bean = bean;
        }

        #region 事件处理
        int pro_set_done_num = 0;

        void OnAllSetDone()
        {
            if (pro_set_done_num == 7)
            {
                Device.BeginInvokeOnMainThread(async () => await DisplayAlert("高级功能设置", "设置完毕", "确定"));
            }
        }

        void ResetAllFlags()
        {
            pro_set_done_num = 0;
        }

        void OnProGet(TweakProType type)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                switch (type.index)
                {
                    case TweakProIndex.BATTERY: cbxProBattery.IsChecked = type.enable; break;
                    case TweakProIndex.GSENSOR: cbxProGsensor.IsChecked = type.enable; break;
                    case TweakProIndex.GNSS: cbxProGnss.IsChecked = type.enable; break;
                    case TweakProIndex.AGPS: cbxProAGPS.IsChecked = type.enable; break;
                    case TweakProIndex.FLASH: cbxProFlash.IsChecked = type.enable; break;
                    case TweakProIndex.SDCARD: cbxProSDcard.IsChecked = type.enable; break;
                    case TweakProIndex.FSYNC: cbxProFsync.IsChecked = type.enable; break;
                }
            });
        }

        void OnProSet(bool success)
        {
            pro_set_done_num += success ? 1 : 0;
            OnAllSetDone();
        }

        async Task<bool> DoProSet()
        {
            bool battery = cbxProBattery.IsChecked;
            bool gsensor = cbxProGsensor.IsChecked;
            bool gnss = cbxProGnss.IsChecked;
            bool agps = cbxProAGPS.IsChecked;
            bool flash = cbxProFlash.IsChecked;
            bool sdcard = cbxProSDcard.IsChecked;
            bool fsync = cbxProFsync.IsChecked;

            await bean.Tweak.SetPro(new TweakProType { index = TweakProIndex.BATTERY, enable = battery, validtime = 19951024 }, bean.Id, bean.LastPowerUtc);
            await bean.Tweak.SetPro(new TweakProType { index = TweakProIndex.GSENSOR, enable = gsensor, validtime = 19951024 }, bean.Id, bean.LastPowerUtc);
            await bean.Tweak.SetPro(new TweakProType { index = TweakProIndex.GNSS, enable = gnss, validtime = 19951024 }, bean.Id, bean.LastPowerUtc);
            await bean.Tweak.SetPro(new TweakProType { index = TweakProIndex.AGPS, enable = agps, validtime = 19951024 }, bean.Id, bean.LastPowerUtc);
            await bean.Tweak.SetPro(new TweakProType { index = TweakProIndex.FLASH, enable = flash, validtime = 19951024 }, bean.Id, bean.LastPowerUtc);
            await bean.Tweak.SetPro(new TweakProType { index = TweakProIndex.SDCARD, enable = sdcard, validtime = 19951024 }, bean.Id, bean.LastPowerUtc);
            await bean.Tweak.SetPro(new TweakProType { index = TweakProIndex.FSYNC, enable = fsync, validtime = 19951024 }, bean.Id, bean.LastPowerUtc);

            return true;
        }
        #endregion

        #region OnAppearing/OnDisappearing
        protected override void OnAppearing()
        {
            base.OnAppearing();

            bean.Tweak.ProGetEvent += OnProGet;
            bean.Tweak.ProSetEvent += OnProSet;

            _ = bean.Tweak.GetPro(TweakProIndex.ALL);
        }

        protected override void OnDisappearing()
        {
            bean.Tweak.ProGetEvent -= OnProGet;
            bean.Tweak.ProSetEvent -= OnProSet;

            base.OnDisappearing();
        }
        #endregion

        #region 交互事件
        async void Button_ProApply_Clicked(object sender, EventArgs e)
        {
            ResetAllFlags();

            await DoProSet();
        }
        #endregion
    }
}
